package cz.data.domain.quality.controller;

import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.JsonPage;
import cz.data.domain.quality.mapstruct.RuleTypeMapper;
import cz.data.domain.quality.model.entity.RuleTypeEntity;
import cz.data.domain.quality.model.entity.RuleTypeReportEntity;
import cz.data.domain.quality.model.query.RuleTypeQuery;
import cz.data.domain.quality.model.vo.RuleTypeVo;
import cz.data.domain.quality.service.RuleTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"规则类型"})
@RestController
@RequestMapping("/ruleTypes")
public class RuleTypeController extends BaseController {

    @Autowired
    private RuleTypeService ruleTypeService;

    @Autowired
    private RuleTypeMapper ruleTypeMapper;

    @ApiOperation(value = "规则类型-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<RuleTypeVo> getRuleTypeById(@PathVariable String id) {
        RuleTypeEntity ruleTypeEntity = ruleTypeService.getRuleTypeById(id);
        return new SimpleDataResponse<>(ruleTypeMapper.toVO(ruleTypeEntity));
    }

    @ApiOperation(value = "规则类型-获取列表")
    @GetMapping("/list")
    public TableDataResponse<RuleTypeEntity> getRuleTypeList() {
        List<RuleTypeEntity> list = ruleTypeService.list(Wrappers.emptyWrapper());
        return new TableDataResponse<>(list);
    }

    @ApiOperation(value = "规则类型-导出-获取列表")
    @GetMapping("/report/list")
    public TableDataResponse<RuleTypeReportEntity> getRuleTypeListForReport() {
        List<RuleTypeReportEntity> list = ruleTypeService.getRuleTypeListForReport();
        return new TableDataResponse<>(list);
    }

    @ApiOperation(value = "规则类型-分页查询")
    @ApiImplicitParam(name = "ruleTypeQuery", value = "查询实体ruleTypeQuery", required = true, dataTypeClass = RuleTypeQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<RuleTypeVo>> getRuleTypePage(RuleTypeQuery ruleTypeQuery) {
        QueryWrapper<RuleTypeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(ruleTypeQuery.getName()), "name", ruleTypeQuery.getName());
        IPage<RuleTypeEntity> page = ruleTypeService.page(new Page<>(ruleTypeQuery.getPageNum(), ruleTypeQuery.getPageSize()), queryWrapper);
        List<RuleTypeVo> collect = page.getRecords().stream().map(ruleTypeMapper::toVO).collect(Collectors.toList());
        JsonPage<RuleTypeVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }
}
